package com.system.connect.springboot_flowers.service.impl;

import com.system.connect.springboot_flowers.entity.Cartitems;
import com.system.connect.springboot_flowers.entity.Flowers;
import com.system.connect.springboot_flowers.entity.Tools;
import com.system.connect.springboot_flowers.mapper.CartitemsMapper;
import com.system.connect.springboot_flowers.service.ICartitemsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.connect.springboot_flowers.service.IFlowersService;
import com.system.connect.springboot_flowers.service.IToolsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 购物车条目表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2025-01-19
 */
@Service
public class CartitemsServiceImpl extends ServiceImpl<CartitemsMapper, Cartitems> implements ICartitemsService {


    @Autowired
    private IFlowersService flowersService;

    @Autowired
    private IToolsService toolsService;

    /*
     * 批量删除
     */
    @Override
    public void batchDelete(List<Cartitems> items) {
        for (Cartitems item : items) {
            this.removeById(item.getItemId());
        }
    }

    /**
     * 批量结算
     *
     * @param items
     * @return
     */
    @Override
    public Double batchCheckout(List<Cartitems> items) {
        if (items == null || items.isEmpty()) {
            return 0.0;
        }
        Double totalPrice = 0.0;
        for (Cartitems item : items) {
            if (item.getItemId() != null) {
                // 如果是鲜花，则根据鲜花ID获取鲜花信息
                Flowers flowers = flowersService.getById(item.getFlowersId());
                Cartitems cartitems = this.getById(item.getFlowersId());
                System.out.println("cartitems.getQuantity():"+cartitems.getQuantity());
                System.out.println("flowers.getPrice():"+flowers.getPrice());
                totalPrice += cartitems.getQuantity() * flowers.getPrice();
            } else if (item.getToolId() != null) {
                // 如果是工具，则根据工具ID获取工具信息
                Tools tools = toolsService.getById(item.getToolId());
                Cartitems cartitems = this.getById(item.getToolId());
               totalPrice += cartitems.getQuantity() * tools.getPrice();
            }
        }

        return totalPrice;
    }

}
